using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.Repositories
{
	#region using
	using AppManagement.Models;
	using AppManagement.Helpers;
	#endregion

	public class TONKHODAUKYRepository
	{
		public static List<TONKHODAUKY> SelectAll()
		{
			try
			{
				return DataContext.Instance.TONKHODAUKY.ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<TONKHODAUKY>();
		}

		public static TONKHODAUKY SelectByID(int id)
		{
			try
			{
				return DataContext.Instance.TONKHODAUKY.FirstOrDefault(item => item.ID == id );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

        public static TONKHODAUKY SelectByThietBiQuiNam(int idthietbi, int qui, int nam)
        {
            try
            {
                return DataContext.Instance.TONKHODAUKY.FirstOrDefault(item => item.Qui == qui && item.Nam == nam && item.IDThietBi == idthietbi);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return null;
        }

		public static bool Insert(TONKHODAUKY obj)
		{
			try
			{
				DataContext.Instance.TONKHODAUKY.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Delete(int id)
		{
			try
			{
				var delitem = DataContext.Instance.TONKHODAUKY.FirstOrDefault(item => item.ID == id );
				DataContext.Instance.TONKHODAUKY.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static List<TONKHODAUKY> RetrieveByID(int id)
		{
			try
			{
				return (from item in DataContext.Instance.TONKHODAUKY where  item.ID == id  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<TONKHODAUKY>();
		}

	}

}
